package cn.rkylin.oms.system.supplier.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.PageInfo;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.common.domain.SortOrderRetriever;
import cn.rkylin.oms.common.dto.PagedData;
import cn.rkylin.oms.system.supplier.domain.SearchConditions;
import cn.rkylin.oms.system.supplier.domain.Supplier;

@Repository
public class SupplierDaoImpl implements ISupplierDao {

    @Autowired
    protected IDataBaseFactory dao;

    @Override
    public void create(Supplier supplier) throws Exception {
        dao.insert("createSupplier", supplier);
    }

    @Override
    public void update(Supplier supplier) throws Exception {
        dao.update("updateSupplier", supplier);
    }

    @Override
    public void delete(String id) throws Exception {
        dao.update("deleteSupplier", id);
    }

    @Override
    public PagedData<Supplier> search(Pageable pageable, SearchConditions conditions) throws Exception {
        PageInfo<Supplier> result = dao.find(pageable.getPageNumber(), pageable.getPageSize(), "searchSupplier", conditions);
        return new PagedData<>(result.getList(), result.getTotal());
    }

    @Override
    public PagedData<Supplier> search(Pageable pageable, Map<String, Object> conditions) throws Exception {
        Map<String, Object> withOrders = new HashMap<>(conditions);
        withOrders.put("orders", SortOrderRetriever.from(pageable));
        PageInfo<Supplier> info = dao.find(pageable.getPageNumber(), pageable.getPageSize(), "clientSearchSupplier", withOrders);
        return new PagedData<>(info.getList(), info.getTotal());
    }
    
    /**
     * 得到供应商列表
     * 
     * @param supplier 供应商bean
     * @return 供应商列表
     * @throws Exception 异常
     */
    public List<Supplier> getSupplierList(Supplier supplier) throws Exception {
       return dao.findAllList("selectSupplierList", supplier);
    }

}
